import 'package:flutter/material.dart';
import 'package:gold/base/provider_scaffold.dart';
import 'package:gold/route/xfg_nav.dart';
import 'package:gold/ui/my/settings/express/express_company_viewmodel.dart';
import 'package:gold/utils/res.dart';
import 'package:gold/utils/xfg_color.dart';
import 'package:gold/utils/xfg_size.dart';
import 'package:gold/widgets/common_widgets.dart';
import 'package:gold/widgets/oval_image.dart';
import 'package:gold/widgets/xfg_text.dart';
import 'package:pull_to_refresh_flutter3/pull_to_refresh_flutter3.dart';

/// @description: 选择快递公司页面
/// @author tangzhentao
/// @created at 19:32 2025/3/8

class ExpressCompanyPage extends StatelessWidget {

  const ExpressCompanyPage({super.key});

  @override
  Widget build(BuildContext context) {
    return ProviderScaffold<ExpressCompanyViewmodel>(
      resizeToAvoidBottomInset: false,
      appBar: (_) => xfgAppbar(title: '选择快递公司'),
      model: ExpressCompanyViewmodel(),
      onInit: (data) => data.loadFirst(),
      toplessBuilder: (_, company) => Container(
        padding: EdgeInsets.all(XfgSize().size10),
        color: XfgColor.white,
        child: Container(
          height: XfgSize().size48,
          padding: EdgeInsets.only(left: XfgSize().size18),
          decoration: loginTextFieldDecoration,
          child: Row(
            children: [
              Expanded(
                child: TextField(
                  keyboardType: TextInputType.text,
                  style: TextStyle(fontSize:XfgSize().text18, color: XfgColor.text1D),
                  maxLines: 1,
                  controller: company.keyController,
                  focusNode: company.ketNode,
                  onEditingComplete: company.search,
                  decoration: InputDecoration(
                      isCollapsed: true,
                      isDense: true,
                      border: InputBorder.none,
                      contentPadding: EdgeInsets.zero,
                      hintStyle: TextStyle(fontSize: XfgSize().text18, color: XfgColor.text3),
                      hintText: '请输入快递公司名称'
                  ),
                ),
              ),
              GestureDetector(
                onTap: company.search,
                behavior: HitTestBehavior.opaque,
                child: Container(
                  width: XfgSize().size48,
                  height: XfgSize().size48,
                  alignment: Alignment.center,
                  child: Image.asset(Res.imagesIntegralSearch, width: XfgSize().size20, height: XfgSize().size20),
                ),
              )
            ],
          ),
        ),
      ),
      builder: (ctx, company) => SmartRefresher(
        controller: company.refreshController,
        enablePullDown: true,
        enablePullUp: true,
        onRefresh: company.refresh,
        onLoading: company.loadMore,
        child: ListView.builder(
          itemCount: company.list.length,
          itemBuilder: (_, index) {
            final model = company.list[index];
            final isLast = index == company.list.length - 1;
            return Container(
              margin: EdgeInsets.fromLTRB(XfgSize().size10, XfgSize().size10, XfgSize().size10, isLast ? (XfgSize().bottomBarHeight == 0 ? XfgSize().size1:XfgSize().bottomBarHeight):0),
              child: Ink(
                decoration: white6Decoration,
                height: XfgSize().size100,
                child: InkWell(
                  borderRadius: BorderRadius.circular(XfgSize().size6),
                  onTap: () => XfgNav.pop(model),
                  child: Padding(
                    padding: EdgeInsets.symmetric(horizontal: XfgSize().size10, vertical: XfgSize().size16),
                    child: Row(
                      children: [
                        OvalImage(model.imgUrl, size: Size.square(XfgSize().size100), radius: BorderRadius.circular(XfgSize().size6), fit: BoxFit.contain),
                        SizedBox(width: XfgSize().size10),
                        Expanded(
                          child: Column(
                            mainAxisAlignment: MainAxisAlignment.spaceBetween,
                            crossAxisAlignment: CrossAxisAlignment.start,
                            children: [
                              XfgText(model.expName?? '', fontSize: XfgSize().text14, color: XfgColor.text1D, bold: FontWeight.bold),
                              XfgText(model.phone?? '', fontSize: XfgSize().text12, color: XfgColor.text1D),
                              GestureDetector(
                                onTap: () => company.toWebBrowser(index),
                                child: XfgText('官网地址', fontSize: XfgSize().text12, decoration: TextDecoration.underline, color: XfgColor.blue.withValues(alpha: 0.9)),
                              )
                            ],
                          ),
                        )
                      ],
                    ),
                  ),
                ),
              ),
            );
          },
        ),
      ),
    );
  }

}
